axios.defaults.baseURL = '../login/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest =function (data) {
    let transObj='';
    for(let i in data){
        transObj+=i+'='+data[i]+'&';
    }
    return transObj;
}

const vm = new Vue({
    el:'#dv',
    data:{
        showLogin: {
            size: 5,
            page: 1,
        },
        id: '',
        name: '',
        password: '',
        upname: '',
        uppassword: '',
        flag: false,
        total: 0,
        list: []
    },
    methods:{
        getAllList(mes){
            axios.post('list.php',mes)
            //transformRequest就是用来处理转换的)
                .then(result => {
                    if(result.status===200){
                        let data = result.data.data.list
                        this.total = result.data.data.count
                        this.list=data
                    }else{
                        alert("服务器出错")
                    }

                })
        },
        update(){
            axios.post('update.php',
                {id: this.id, name: this.upname, password: this.uppassword})
                .then(result => {
                    if (result.status === 200) {
                        this.flag = false
                        this.getAllList(this.showLogin)
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        del(id){
            axios.post('delete.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        alert("删除成功")
                        this.getAllList(this.showLogin)
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        modify(id){
            this.flag = true
            this.id = id
            axios.post('get.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        let data = result.data.data
                        this.upname = data.name
                        this.uppassword = data.password
                    } else {
                        this.upname = this.uppassword = ''
                    }
                })
        },
        next(){
            this.showLogin.page += 1
            if (this.showLogin.page > Math.ceil(this.total / this.showLogin.size)) {
                return
            }
            this.getAllList(this.showLogin)
        },
        prev(){
            this.showLogin.page -= 1
            if (this.showLogin.page < 1) {
                return
            }
            this.getAllList(this.showLogin)
        },
        choosePage(index){
            this.showLogin.page = index
            this.getAllList(this.showLogin)
        }
    },
    created(){
        this.getAllList(this.showLogin)
    }
})

